package net.tailfish.api.base.result;

import net.tailfish.api.base.enums.SnowEnum;
import net.tailfish.api.base.exception.SnowException;
import net.tailfish.api.base.response.SnowResponse;

/**
 * @PackageName net.tailfish.api.base.result
 * @ClassName SnowResult
 * @Author tailFish
 * @Date Created in 18:16 2021/5/28
 * @Description 响应工具处理类
 */
public class SnowResult {

    public SnowResult() {}

    /**
     * 错误响应
     * @param e 自定义异常
     * @return {{SnowResponse}}
     */
    public static SnowResponse error(SnowException e){
        return error(e.getCode(),e.getMessage());
    }

    /**
     * 错误响应
     * @param msg 响应说明
     * @return {{SnowResponse}}
     */
    public static SnowResponse error(String msg){
        return error(SnowEnum.SYSTEM_ERROR.getCode(),msg);
    }

    /**
     * 错误响应
     * @param snowEnum 响应枚举
     * @return {{SnowResponse}}
     */
    public static SnowResponse error(SnowEnum snowEnum){
        return error(snowEnum.getCode(),snowEnum.getMsg());
    }

    /**
     * 错误响应
     * @param code 响应码
     * @param msg 响应说明
     * @return {{SnowResponse}}
     */
    public static SnowResponse error(String code,String msg){
        return basic(code,msg,null);
    }

    /**
     * 成功响应
     * @param body 响应体
     * @return {{SnowResponse}}
     */
    public static SnowResponse success(Object body){
        return success(SnowEnum.OK.getCode(),SnowEnum.OK.getMsg(),body);
    }

    /**
     * 成功响应
     * @param code 响应码
     * @param msg 响应说明
     * @param body 响应体
     * @return {{SnowResponse}}
     */
    public static SnowResponse success(String code,String msg,Object body){
        return basic(code,msg,body);
    }

    private static SnowResponse basic(String code,String msg,Object body){
        SnowResponse response = new SnowResponse();

        response.setCode(code);
        response.setMsg(msg);
        response.setBody(body);

        return response;
    }
}
